//
//  SNSexView.m
//  project
//
//  Created by 黄建华 on 2020/12/6.
//  Copyright © 2020 黄建华. All rights reserved.
//

#import "SNSexView.h"

@implementation SNSexView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self createUi];
    }
    return self;
}

- (void)createUi
{
    self.layer.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:246/255.0 alpha:1.0].CGColor;
    self.layer.masksToBounds = YES;
    
    UIView *view = [[UIView alloc] initWithFrame:SNFRAME(0, 0, screenWidth, 44)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    UILabel *titleLabel = [UILabel simpleWithRect:SNFRAME(0, 0, screenWidth, 44) withAlignment:NSTextAlignmentCenter withFontSize:16 withText:SN_Text(@"性别选择") withTextColor:UIColorHEX(0x161616)];
    titleLabel.font = SNFONTMEDIUM(16);
    [self addSubview:titleLabel];
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:SNFRAME(15, 23+44, screenWidth-30, 50*2)];
    view1.backgroundColor = [UIColor whiteColor];
    view1.layer.cornerRadius = 9;
    view1.layer.masksToBounds = YES;
    [self addSubview:view1];
    
 
    
    UIButton *btn = [[UIButton alloc] initWithFrame:SNFRAME(screenWidth/2-90, view1.bottom +23, 180, 36)];
    [btn setTitle:SN_Text(@"确定") forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.backgroundColor = UIColorHEX(0xFF7A00);
    btn.layer.cornerRadius = 6;
    [self addSubview:btn];
    
    [btn addTouchUpTarget:self action:@selector(btnTouchInWith)];
    if (SN_USER.gender == 1) {
        _index = 1;
    }
    if (SN_USER.gender == 2) {
        _index = 2;
    }
    
    for (int i = 0; i <2; i++) {
        UIView *bgView = [[UIView alloc]initWithFrame:SNFRAME(0, i*50,view1.width, 50)];
        UILabel *label = [UILabel simpleWithRect:SNFRAME(20,15, 120, 20) withAlignment:NSTextAlignmentLeft withFontSize:17 withText:SN_Text(@"男生") withTextColor:UIColorHEX(0x161616)];
        [bgView addSubview:label];
        
        
        if (i ==1) {
            UIView *line = [[UIView alloc] initWithFrame:SNFRAME(0,50,view1.width, 0.5)];
            line.backgroundColor = UIColorHEX(0xd9d9d9);
            [view1 addSubview:line];
            label.text = SN_Text(@"女生");
        }
        
        [view1 addSubview:bgView];
        
        
        if (i == 0) {
            _btn1 = [[UIButton alloc] initWithFrame:SNFRAME(view1.width -40,16, 22, 22)];
            _btn1.layer.borderWidth = 1;
            _btn1.layer.masksToBounds = YES;
            _btn1.layer.cornerRadius = 11;
            _btn1.layer.borderColor = UIColorHEX(0xC7C7CB).CGColor;
            [_btn1 addTouchUpTarget:self action:@selector(btnTouchInWith1)];
            [bgView addSubview:_btn1];
        }
        if (i == 1) {
            _btn2 = [[UIButton alloc] initWithFrame:SNFRAME(view1.width -40,16, 22, 22)];
            _btn2.layer.borderWidth = 1;
            _btn2.layer.masksToBounds = YES;
            _btn2.layer.cornerRadius = 11;
            _btn2.layer.borderColor = UIColorHEX(0xC7C7CB).CGColor;
            [_btn2 addTouchUpTarget:self action:@selector(btnTouchInWith2)];
            [bgView addSubview:_btn2];
        }
        
    }
    [self updateBtnState];
}

- (void)btnTouchInWith1
{
    _index = 1;
    SN_USER.gender = 1;
    [self updateBtnState];
    
}

- (void)btnTouchInWith2
{
    _index = 2;
    SN_USER.gender = 2;
    [self updateBtnState];
}

- (void)updateBtnState
{
    
    _btn1.backgroundColor = [UIColor clearColor];
    _btn2.backgroundColor = [UIColor clearColor];
    if (SN_USER.gender == 1) {
        _btn1.backgroundColor = UIColorHEX(0xFF955B);
    }
    if (SN_USER.gender == 2) {
        _btn2.backgroundColor = UIColorHEX(0xFF955B);
    }
}

- (void)btnTouchInWith
{
    
//    NSString *string = SN_USER.gender?SN_Text(@"女"):SN_Text(@"男");
    
    if (SN_USER.gender == 1) {
        if (_backBlock) {
            _backBlock(SN_Text(@"男"));
        }
    }
    if (SN_USER.gender == 2) {
        if (_backBlock) {
            _backBlock(SN_Text(@"女"));
        }
    }

}

@end
